2020-2021 Sunseeker Telemetry and Lighting System
ctsd16_ex5_threeChSingleConv.c
Go to the documentation of this file.
1 /* --COPYRIGHT--,BSD
2  * Copyright (c) 2017, Texas Instruments Incorporated
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions
7  * are met:
8  *
9  * * Redistributions of source code must retain the above copyright
10  * notice, this list of conditions and the following disclaimer.
11  *
12  * * Redistributions in binary form must reproduce the above copyright
13  * notice, this list of conditions and the following disclaimer in the
14  * documentation and/or other materials provided with the distribution.
15  *
16  * * Neither the name of Texas Instruments Incorporated nor the names of
17  * its contributors may be used to endorse or promote products derived
18  * from this software without specific prior written permission.
19  *
20  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
22  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
23  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
24  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
25  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
26  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
27  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
28  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
29  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
30  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31  * --/COPYRIGHT--*/
32 //******************************************************************************
33 // MSP430FG662x Demo - CTSD16, Single Conversion of 3 Input Channels
34 //
35 // Description: This program uses the CTSD16 module to perform a single
36 // conversion on 3 input channels (0, 1, and 2). A CTSD16 interrupt occurs when
37 // the conversions have completed.
38 //
39 // Test by applying voltages to the input channel and setting a breakpoint
40 // at the indicated line. Run program until it reaches the breakpoint, then use
41 // the debugger's watch window to view the conversion result.
42 //
43 // Results (upper 16 bits only) are stored in the arrays "Chresults", with the
44 // input channel number equal to the value's index (ex. input channel 0 stored
45 // at Chresults[0]).
46 //
47 // ACLK = 32kHz, MCLK = SMCLK = Calibrated DCO = 16.384MHz, SD_CLK = 1.024MHz
48 // * Ensure low_level_init.c is included when building/running this example *
49 //
50 // Notes: For minimum Vcc required for CTSD16 module - see datasheet
51 // 1nF cap btw Vref and AVss is recommended when using 1.2V ref
52 //
53 // MSP430FG662x
54 // -----------------
55 // /|\| |
56 // | | |
57 // --|RST |
58 // | |
59 // Vin1+ -->|P6.4/AD0+ VREF |---+
60 // Vin1- -->|P6.5/AD0- | |
61 // Vin2+ -->|P6.6/AD0+ | -+- 1nF
62 // Vin2- -->|P6.7/AD0- | -+-
63 // Vin3+ -->|P7.4/AD0+ | |
64 // Vin3- -->|P7.5/AD0- AVss |---+
65 //
75 //******************************************************************************
76 #include "inc/hw_memmap.h"
77 #include "wdt_a.h"
78 #include "gpio.h"
79 #include "ctsd16.h"
80 
81 #define Num_of_Channels 3
82 
83 /* Arrays to store CTSD16 conversion results */
85 uint8_t i = 0;
86 uint8_t channels[Num_of_Channels] = {CTSD16_INPUT_CH9, CTSD16_INPUT_CH11, CTSD16_INPUT_CH13};
87 
88 void main(void) {
89  // Stop WDT
90  WDT_A_hold(WDT_A_BASE);
91 
92  // Select AD0+/- analog input pins
93  GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P6, GPIO_PIN4 | GPIO_PIN5 |
94  GPIO_PIN6 | GPIO_PIN7);
95  GPIO_setAsPeripheralModuleFunctionInputPin(GPIO_PORT_P7, GPIO_PIN4 | GPIO_PIN5);
96 
97  // Initialize CTSD16 using internal reference and internal resistor for clock
98  CTSD16_init(CTSD16_BASE,
99  CTSD16_RTR_INPUT_CHARGEPUMP_BURST_REQUEST_DISABLE, CTSD16_REF_INTERNAL);
100 
101  // Initialize converter 0: AD0+ / AD0- as input, channel 9
102  CTSD16_initConverter(CTSD16_BASE, CTSD16_CONVERTER_0, CTSD16_SINGLE_MODE,
103  CTSD16_INPUT_CH9);
104 
105  // Clear converter 0 interrupt flags
106  CTSD16_clearInterrupt(CTSD16_BASE, CTSD16_CONVERTER_0,
107  CTSD16_CONVERTER_INTERRUPT);
108  // Enable result interrupts
109  CTSD16_enableInterrupt(CTSD16_BASE, CTSD16_CONVERTER_0,
110  CTSD16_CONVERTER_INTERRUPT);
111 
112  // Delay ~120us for 1.2V ref to settle
113  __delay_cycles(2000);
114 
115  while(1) {
116  // Set bit to start conversion
117  CTSD16_startConverterConversion(CTSD16_BASE, CTSD16_CONVERTER_0);
118  __bis_SR_register(LPM0_bits | GIE); // Enter LPM0 w/ interrupts
119  __no_operation(); // For debugger
120 
121  if (i >= Num_of_Channels) {
122  i = 0;
123  }
124  CTSD16_setInputChannel(CTSD16_BASE, CTSD16_CONVERTER_0, channels[i]);
125  }
126 }
127 
128 #if defined(__TI_COMPILER_VERSION__) || defined(__IAR_SYSTEMS_ICC__)
129 #pragma vector=CTSD16_VECTOR
130 __interrupt
131 #elif defined(__GNUC__)
132 __attribute__((interrupt(CTSD16_VECTOR)))
133 #endif
134 void CTSD16_ISR(void) {
135  switch (__even_in_range(CTSD16IV,CTSD16IV_CTSD16MEM0)) {
136  case CTSD16IV_NONE: break;
137  case CTSD16IV_CTSD16OVIFG: break;
138  case CTSD16IV_CTSD16MEM0:
139  // Save CH0 results (clears IFG)
140  Chresults[i++] = CTSD16_getResults(CTSD16_BASE, CTSD16_CONVERTER_0);
141  __bic_SR_register_on_exit(LPM0_bits); // Wake up
142  break;
143  default: break;
144  }
145 }
uint32_t Chresults[Num_of_Channels]
uint8_t channels[Num_of_Channels]
void main(void)
#define Num_of_Channels
void CTSD16_ISR(void)
__no_operation()
__bic_SR_register_on_exit(LPM3_bits|GIE)
__delay_cycles(500000)